import json

from config import *
from DescribeDomainRecords import Get_DescribeDomainRecords
from UpdateDomainRecord import Post_UpdateDomainRecord
from MyIP import Get_MyIP


def Config_Operation():
    jsonconfig = open("config.json", 'r+')

    jscode = str(jsonconfig.read())
    text = json.loads(jscode)
    # print(type(text))
    # print(text["query_RR"])
    jsonconfig.close()
    for i in text["query_RR"]:
        print("当前正在处理:"+i+"域名项")
        CheckAndChangeIP(query_RR=i)









def CheckAndChangeIP(query_RR):
    DomainRecords = Get_DescribeDomainRecords(accessKeyId,
                                              accessSecret,
                                              city,
                                              query_RR,
                                              query_DomainName)

    MyIPAddress = Get_MyIP()

    print("DNS存储的IP:"+DomainRecords[1], "(～￣▽￣)～ 本机的IP："+MyIPAddress)
    if DomainRecords[1] == MyIPAddress:
        print("IP地址相同无需更新")

    else:
        try:
            Post_UpdateDomainRecord(accessKeyId,
                                    accessSecret,
                                    city,
                                    write_TTL,
                                    write_Type,

                                    write_Value=MyIPAddress,
                                    RecordId=DomainRecords[0],
                                    RR=query_RR)

            print("IP已经成功修改为:" + MyIPAddress)
        except Exception as ex:
            print("发生错误了,出现如下异常%s" % ex)

if __name__ == "__main__":
    Config_Operation()